var http = require("http");
var fs = require("fs");
var url = require("url");
var port = process.argv[2];

if (!port) {
  console.log("请指定端口好不好\n node server.js 8888 这样不会吗？");
  process.exit(1);
}

var server = http.createServer(function (request, response) {
  var parseUrl = url.parse(request.url, true);
  var pathWithQuery = request.url;
  var queryString = "";
  if (pathWithQuery.indexOf("?") >= 0) {
    queryString = pathWithQuery.substring(pathWithQuery.indexOf("?"));
  }
  var path = parseUrl.pathname;
  var query = parseUrl.query;
  var method = request.method;

  console.log("接收请求，路径为：" + pathWithQuery);

  if (path === "/index.html") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    let string = fs.readFileSync('public/index.html').toString()
    const page1 = fs.readFileSync('db/page1.json').toString()
    const array = JSON.parse(page1)
    const result = array.map(item => `<li>${item.id}</li>`).join('')

    string = string.replace('{{page1}}', `<ul id="pageData">${result}</ul>`)
    response.write(string);
    response.end();
  } else if (path === "/main.js") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/javascript;charset=utf-8");
    response.write(fs.readFileSync('public/main.js'));
    response.end();
  } else if (path === "/style.css") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/css;charset=utf-8");
    response.write(fs.readFileSync('public/style.css'));
    response.end();
  } else if (path === "/app.js") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/javascript;charset=utf-8");
    response.write(fs.readFileSync('public/app.js'));
    response.end();
  } else if (path === "/page.html") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    response.write(fs.readFileSync('public/page.html'));
    response.end();
  } else if (path === "/info.xml") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/xml;charset=utf-8");
    response.write(fs.readFileSync('public/info.xml'));
    response.end();
  } else if (path === "/data.json") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/json;charset=utf-8");
    response.write(fs.readFileSync('public/data.json'));
    response.end();
  } else if (path === "/page2") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/json;charset=utf-8");
    response.write(fs.readFileSync('db/page2.json'));
    response.end();
  } else if (path === "/page3") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/json;charset=utf-8");
    response.write(fs.readFileSync('db/page3.json'));
    response.end();
  } else if (path === "/qian") {
    response.statusCode = 200;
    response.setHeader("Content-Type", "text/javascript;charset=utf-8");
    response.write(`console.log('这是JS内容')`);
    response.end();
  } else {
    response.statusCode = 404;
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    response.write(`      
    <!DOCTYPE html>
    <head>
      <link rel="stylesheet" href="/style">
    </head>
    <body>
      <h1>404</h1>
      <h2>你访问的页面不存在</h2>
    </body>`);
    response.end();
  }
});

server.listen(port);
console.log(
  "监听 " +
    port +
    " 成功\n请在单杠上做后摆上转体360单手倒立然后另一只手打开 http://localhost:" +
    port
);